﻿/**
 * @author sminutoli
 * version 1.0
 * Vamos a tener que revisar la prolijidad de este asunto
 */
import com.dinmax.flash.draw.cabeza.*
import com.dinmax.flash.central.extension.user.*
import com.dinmax.flash.utilities.textfield.TextFieldUtils


class com.dinmax.flash.central.bridge.ContactABMBridge
extends ControlCabeza
implements SelectedListener, ChangedListener {
	
	private var mcDialog:DialogCabeza;
	
	private var mcNickField:FieldCabeza;
	private var mcNameField:FieldCabeza;
	private var mcLastNameField:FieldCabeza;
	private var mcPhoneField:FieldCabeza;
	private var mcEmailField:FieldCabeza;
		
	private var mcTypeButton:ButtonCabeza;
	private var mcAcceptButton:ButtonCabeza;
	private var mcDeleteButton:ButtonCabeza;
	
	
	
	public function initChild(control:ControlCabeza):Void{
		var cnt:Contact = Contact(this.getValue());
		switch(control){
			case this.mcNickField:
			FieldCabeza(control).setLabel("Nick");
			FieldCabeza(control).setValue(cnt.getNick());
			break;
			
			case this.mcNameField:
			FieldCabeza(control).setLabel("Nombre");
			FieldCabeza(control).setValue(cnt.getName());
			break;
			
			case this.mcLastNameField:
			FieldCabeza(control).setLabel("Apellido");
			FieldCabeza(control).setValue(cnt.getLastName());
			
			break;
			
			case this.mcPhoneField:
			FieldCabeza(control).setLabel("Telefono");
			//trace("ContactABMBridge.as initChild() mcPhoneField phone= "+cnt.getPhone());
			FieldCabeza(control).setValue(String(cnt.getPhone()));
			
			break;
			
			case this.mcEmailField:
			FieldCabeza(control).setLabel("Email");
			FieldCabeza(control).setValue(cnt.getEmail());
			
			break;
			
			case this.mcTypeButton:
			control.setValue(cnt.getVisibility());
			if(control.getValue() == Contact.PRIVATE){
				control.gotoAndStop(2);
			} else {
				control.setValue(Contact.PUBLIC);
				control.gotoAndStop(1);
			};
			break;
			
			case this.mcDialog:
			control._visible = false;
			break;
		};
		control.addListener(this);
		return;
	};
	
	private function validate():Void {
		try {
			var fields:Array=[mcNickField, mcNameField, mcLastNameField, mcPhoneField, mcEmailField];
			for (var i : Number = 0; i < fields.length; i++) {
				TextFieldUtils.validateField(FieldCabeza(fields[i]).getField());
			}
			this.setValue(createContact());
			mcDialog.setLabel("¿Desea guardar las modificaciones?");
			mcDialog._visible = true;
		} catch (e) {
			trace("ContactABMBridge.as validate() ERROR= "+e);
		};
		
	};
	
	public function selected(control:ControlCabeza):Void {
		switch(control){
			case this.mcTypeButton:
				if(this.mcTypeButton._currentframe == 1){
					this.mcTypeButton.gotoAndStop(2);
					this.mcTypeButton.setValue(Contact.PRIVATE);
				} else {
					this.mcTypeButton.gotoAndStop(1);
					this.mcTypeButton.setValue(Contact.PUBLIC);
				};			
			break;
			
			case this.mcAcceptButton:
			this.validate();
			break;
			
			case this.mcDeleteButton:
			this.setValue(Contact(this.getValue()).getId());
			mcDialog.setLabel("¿Desea eliminar al contacto?");
			mcDialog._visible = true;
			//this.selectEvent();
			break;
			
			case this.mcDialog:
			//trace("ContactABMBridge.as selected() mcDialog");
			//trace("ContactABMBridge.as selected() mcDialog.getValue() "+mcDialog.getValue());
			if(control.getValue() != undefined){
				this.selectEvent();
			} else {
				this.setValue(undefined);
				this.selectEvent();
			};
			break;		
		};
		return;
	};
		
	public function changed(control:ControlCabeza, type:String):Void {
		//trace("changed "+control);
		return;
	};
	
	private function createContact():Contact {
		var cnt:Contact = new Contact();
		cnt.setId(Contact(this.getValue()).getId());
		cnt.setName(mcNameField.getValue());
		cnt.setLastName(mcLastNameField.getValue());
		cnt.setNick(mcNickField.getValue());
		cnt.setEmail(mcEmailField.getValue());
		cnt.setPhone(Number(mcPhoneField.getValue()));
		cnt.setVisibility(String(mcTypeButton.getValue()));
		return cnt;
	};
};